(function($){
	
	var KEY = "jforest.progress";
	/*
		参数：displayEl : true|false //是否显示定时器
	*/
	var Progress = function(element,options){
		this.$el = $(element);
		this.options = $.extend({},{
			callback:function(){},
			active:true,
			interval:1,
			startRate:0,
			workRate:0,
			totalRate:100,
			intervalTime:1000,
			className:'progress-bar-success'
		},options);
		this._createEl();
	}
	
	Progress.prototype = {
		_createEl:function(){
			var me = this,opts = this.options;
			opts.currentRate = opts.startRate;
			this.$el.empty();
			var rate = opts.currentRate/opts.totalRate * 100;
			this.$el.append('<div class="progress" style="margin-bottom:0;"><div class="progress-bar '+opts.className+'" style="width:'+rate+'%;">'
					     	+rate+'%</div></div>');
			this.clear();
			this.timer = setInterval(function(){
				if(opts.active == true){
					if(opts.workRate > opts.currentRate){
						opts.currentRate = opts.currentRate + opts.interval;
						rate = opts.currentRate/opts.totalRate * 100;
						if(rate > 100){rate = 100;}
						me.$el.find('.progress-bar').width(rate+"%").html(opts.currentRate);
					}
				}
			},opts.intervalTime);
		},
		clear:function(){
			if(this.timer){
				clearInterval(this.timer);
				delete this.timer;
				console.log('清理Progress定时器');
			}
		},
		setWorkRate:function(workRate){
			this.options.workRate = workRate || 0;
		},
		setStartRate:function(startRate){
			if(this.options.startRate != startRate){
				this.options.startRate = startRate || 0;
				this.options.currentRate = startRate || 0;
				var rate = this.options.currentRate/this.options.totalRate * 100;
				if(rate > 100){rate = 100;}
				this.$el.find('.progress-bar').width(rate+"%").html(this.options.currentRate);
			}
		},
		setRate:function(totalRate,startRate,workRate){
			if(this.options.totalRate != totalRate || this.options.startRate != startRate){
				this.options.totalRate = totalRate || 0;
				this.options.startRate = startRate || 0;
				this.options.currentRate = startRate || 0;
				var rate = this.options.currentRate/this.options.totalRate * 100;
				if(rate > 100){rate = 100;}
				this.$el.find('.progress-bar').width(rate+"%").html(this.options.currentRate);
			}
			this.options.workRate = workRate || 0;
			
		},
		close:function(){
			this.options.active = false;
		},
		open:function(){
			this.options.active = true;
		},
		setClassName:function(className){
			this.$el.find('.progress-bar').removeClass(this.options.className).addClass(className);
			this.options.className = className;
		}
	}
	
	$.fn.jfProgress = function(){
		var args = Array.prototype.slice.call(arguments, 0);
		var value = undefined;
		this.each(function () {
			value = $(this).data(KEY);
			if (args.length === 0 || typeof(args[0]) === "object") {
				if(!value){
					opts = args.length === 0 ? {} : $.extend({}, args[0]);
					value = new Progress(this,opts);
					$(this).data(KEY,value); //创建控件
				}
			}else if (typeof(args[0]) === "string"){
				if(!value){return;}
				value = value[args[0]].apply(value, args.slice(1));
			}else{
				 throw 'Invalid arguments to '+KEY+' plugin: ' + args;
			}
	    });
	    return (value === undefined) ? this : value;
	};
	
	$.fn.jfProgress.Constructor = Progress;
	
})(window.jQuery);